<?php

namespace services\log;

use common\components\Service;
use common\helpers\DateHelper;
use common\models\base\PatientDiseases;
use common\models\clinic\Clinic;
use common\models\marketing\ClinicAssess;
use common\models\marketing\EffectType;
use common\models\marketing\OutpatientType;
use common\models\marketing\RiskType;
use common\models\marketing\SourceChannel;
use common\models\member\Member;
use common\models\patient\OperateLog;
use common\models\patient\User;
use Yii;

class PatientOperateLogService extends Service
{
    /**
     * @param int $porjectId
     * @param int $patientId
     * @param int $actionType
     * @param string $content
     * @return void
     * @throws \Exception
     */
    public static function create(int $porjectId, int $patientId, int $actionType, string $content, int $isSystem = 0): void
    {
        if (empty($content)) {
            return;
        }
        $operateLog = new OperateLog();
        $operateLog->project_id = $porjectId;
        $operateLog->patient_user_id = $patientId;
        $operateLog->action_type = $actionType;
        $operateLog->action_content = $content;
        if ($isSystem) {
            $operateLog->operate_id = 0;
            $operateLog->operate_name = '系统';
        } else {
            $operateLog->operate_id = Yii::$app->user->identity->member_id;
            $operateLog->operate_name = Yii::$app->user->identity->member->realname;
        }
        $operateLog->saveAndCheckResult();
    }

    /**
     * 获取患者变动日志
     * @param User $new
     * @param User|null $old
     * @return string
     */
    public static function getPatientChangeLog(User $new, ?User $old): string
    {
        $actionContent = '';
        foreach ($new->getAttributes() as $key => $val) {
            $oldVal = $old[$key];
            if ($oldVal != $val) {
                switch ($key) {
                    case 'name':
                        $actionContent .= "患者姓名由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'birthday':
                        $oldVal = $oldVal == '0000-00-00' ? '' : $oldVal;
                        $actionContent .= "患者生日由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'age':
                        $actionContent .= "患者年龄由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'nickname':
                        $actionContent .= "患者微信昵称由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'wx_name':
                        $actionContent .= "患者微信号由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'source_channel_id':
                        $oldValStr = $oldVal ? SourceChannel::findOne($oldVal)->source_name ?? '' : '';
                        $newValStr = $new->source_channel_id ? SourceChannel::findOne($new->source_channel_id)->source_name ?? '' : '';
                        $actionContent .= "患者来源渠道由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'gender':
                        $oldValStr = User::$genders[$oldVal] ?? '';
                        $newValStr = User::$genders[$val] ?? '';
                        $actionContent .= "患者性别由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'mobile':
                        $actionContent .= "患者手机号由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'city_name':
                        $actionContent .= "患者城市由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'address':
                        $actionContent .= "患者详细地址由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'patient_diseases_id':
                        $oldValStr = $oldVal ? PatientDiseases::findOne($oldVal)->diseases_name ?? '' : '';
                        $newValStr = $new->patient_diseases_id ? PatientDiseases::findOne($new->patient_diseases_id)->diseases_name ?? '' : '';
                        $actionContent .= "患者病种由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'patient_body_parts_name':// 部位
                        $actionContent .= "患者部位由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'patient_tags_name':// 标签
                        $actionContent .= "患者标签由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'belong_assistant_id':
                        $oldValStr = $oldVal ? Member::findOne($oldVal)->realname ?? '' : '';
                        $newValStr = $new->belong_assistant_id ? Member::findOne($new->belong_assistant_id)->realname ?? '' : '';
                        $actionContent .= "患者医助归属人由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'belong_member_id':
                        break;
                    case 'belong_member_name':
                        $actionContent .= "患者客服归属人由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'effect_type_id':
                        $oldValStr = $oldVal ? EffectType::findOne($oldVal)->effect_name ?? '' : '';
                        $newValStr = $new->effect_type_id ? EffectType::findOne($new->effect_type_id)->effect_name ?? '' : '';
                        $actionContent .= "患者效果类型由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'risk_type_id':
                        $oldValStr = $oldVal ? RiskType::findOne($oldVal)->risk_name ?? '' : '';
                        $newValStr = $new->risk_type_id ? RiskType::findOne($new->risk_type_id)->risk_name ?? '' : '';
                        $actionContent .= "患者风控类型由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'clinic_assess_id':
                        $oldValStr = $oldVal ? ClinicAssess::findOne($oldVal)->assess_name ?? '' : '';
                        $newValStr = $new->clinic_assess_id ? ClinicAssess::findOne($new->clinic_assess_id)->assess_name ?? '' : '';
                        $actionContent .= "患者诊疗评估由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'outpatient_type_id':
                        $oldValStr = $oldVal ? OutpatientType::findOne($oldVal)->outpatient_name ?? '' : '';
                        $newValStr = $new->outpatient_type_id ? OutpatientType::findOne($new->outpatient_type_id)->outpatient_name ?? '' : '';
                        $actionContent .= "患者就诊类型由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'clinic_id':
                        $oldValStr = $oldVal ? Clinic::findOne($oldVal)->name ?? '' : '';
                        $newValStr = $new->clinic_id ? Clinic::findOne($new->clinic_id)->name ?? '' : '';
                        $actionContent .= "患者诊室由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'profession':
                        $actionContent .= "患者职业由【{$oldVal}】变更为【{$val}】;";
                        break;
                    case 'next_callback_time':
                        $oldValStr = DateHelper::timestampToDate((int)$oldVal, DateHelper::DATE_FORMAT_1);
                        $newValStr = DateHelper::timestampToDate($new->next_callback_time, DateHelper::DATE_FORMAT_1);
                        $actionContent .= "下次回访时间由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'next_revisiting_time':
                        $oldValStr = DateHelper::timestampToDate((int)$oldVal, DateHelper::DATE_FORMAT_1);
                        $newValStr = DateHelper::timestampToDate($new->next_revisiting_time, DateHelper::DATE_FORMAT_1);
                        $actionContent .= "下次复诊时间由【{$oldValStr}】变更为【{$newValStr}】;";
                        break;
                    case 'remark':
                        $actionContent .= "患者备注由【{$oldVal}】变更为【{$val}】;";
                        break;
                    default:
                        break;
                }
            }
        }
        return $actionContent;
    }
}
